const http = require('http')
const https = require('https')
const cheerio = require('cheerio')
const url = require('url')
const { isElementNode } = require('parse5-htmlparser2-tree-adapter')
var reqUrl = "https://maoyan.com/films?showType=3"
var options = url.parse(reqUrl)


http.createServer((req, res) => {
    var requrl = req.url

    // if (req.url == '/') {
    //     var fileContent = fs.readFileSync(pagePath)
    //     res.writeHead(200, { 'Content-Type': 'text/html;charset:utf-8' })
    //     res.write(fileContent)
    //     res.end()
    // }
    // else if (req.url == '/getlist') {

    var request = https.request(options, (res) => {
        var str = ''

        res.on('data', (chunk) => {
            str += chunk
        })

        res.on('end', () => {
            console.log(str)
            var $ = cheerio.load(str)
            console.log($);
            var movies = []

            for (var i = 0; i < $('.movie-list').find('dd').length; i++) {
                movies[i] = {}
            }
            console.log($('.movie-list').find('dd').length);
            $('.movie-item-title a').each((i, el) => {
                movies[i].movieName = $(el).text()
            })

            $('.movie-item-title a').each((i, el) => {
                movies[i].movieId = $(el).attr('data-val').split(':').slice(0, -1)
            })

            var sum=0
            $('.channel-detail-orange').each((i, el) => {
                movies[i].movieRange = $(el).children().first().text() + $(el).children().last().text() ? 
                    $(el).children().first().text() + $(el).children().last().text() : '暂无评价'
                ++sum;
                console.log(sum)
            })

            //    console.log(moives)
            // res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'})
            // res.write(movies)
            // res.end()
            // }
        })
    })
    request.end()
    // }


}).listen(8080)

console.log('server')